package com.tcfvic.client.panels.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.tcfvic.client.PersonService;
import com.tcfvic.client.PersonServiceAsync;
import com.tcfvic.client.panels.widgets.addons.advancedtable.AdvancedTable;
import com.tcfvic.client.panels.widgets.addons.advancedtable.DataFilter;
import com.tcfvic.client.panels.widgets.addons.advancedtable.TableModelServiceAsync;

public class RTODetailsListWidget extends FlexTable {

	private final PersonServiceAsync personService = GWT
			.create(PersonService.class);

	private TextBox nameTextBox = new TextBox();
	private TextBox contactNameTextBox = new TextBox();
	private TextBox contactPosition = new TextBox();
	private TextBox campus = new TextBox();
	private TextBox state = new TextBox();
	private AdvancedTable at = new AdvancedTable();

	public RTODetailsListWidget(TableModelServiceAsync tableModelServiceAsync,
			final RTODetailsInputWidget rowSelectionListener) {
		super();
		at = new AdvancedTable();

		// Create a table to layout the form options
		setCellSpacing(4);
		setWidth("100%");
		FlexCellFormatter cellFormatter = getFlexCellFormatter();

		at.setFirstColumnVisible(false);
		at.setPageSize(4);
		// at.setHeight("300 px");
		at.setWidth("100%");
		at.addRowSelectionListener(rowSelectionListener);
		at.setPageSize(4);

		// last action to be performed
		at.setTableModelService(tableModelServiceAsync);

		nameTextBox.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[5];
					df[0] = new DataFilter("name", filter.getText());
					df[1] = new DataFilter("contactName", contactNameTextBox
							.getText());
					df[2] = new DataFilter("contactPosition", contactPosition
							.getText());
					df[3] = new DataFilter("campus", campus.getText());
					df[4] = new DataFilter("state", state.getText());
					at.applyFilters(df);
				}
			}
		});

		contactNameTextBox.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[5];
					df[0] = new DataFilter("name", nameTextBox.getText());
					df[1] = new DataFilter("contactName", filter.getText());
					df[2] = new DataFilter("contactPosition", contactPosition
							.getText());
					df[3] = new DataFilter("campus", campus.getText());
					df[4] = new DataFilter("state", state.getText());
					at.applyFilters(df);
				}
			}
		});

		contactPosition.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[5];
					df[0] = new DataFilter("name", nameTextBox.getText());
					df[1] = new DataFilter("contactName", contactNameTextBox
							.getText());
					df[2] = new DataFilter("contactPosition", filter.getText());
					df[3] = new DataFilter("campus", campus.getText());
					df[4] = new DataFilter("state", state.getText());
					at.applyFilters(df);
				}
			}
		});

		campus.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[5];
					df[0] = new DataFilter("name", nameTextBox.getText());
					df[1] = new DataFilter("contactName", contactNameTextBox
							.getText());
					df[2] = new DataFilter("contactPosition", contactPosition
							.getText());
					df[3] = new DataFilter("campus", filter.getText());
					df[4] = new DataFilter("state", state.getText());
					at.applyFilters(df);
				}
			}
		});

		state.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[5];
					df[0] = new DataFilter("name", nameTextBox.getText());
					df[1] = new DataFilter("contactName", contactNameTextBox
							.getText());
					df[2] = new DataFilter("contactPosition", contactPosition
							.getText());
					df[3] = new DataFilter("campus", campus.getText());
					df[4] = new DataFilter("state", filter.getText());
					at.applyFilters(df);
				}
			}
		});

		// Add some standard form options
		Button newButton = new Button("Create new");
		newButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				rowSelectionListener.createNewRecord();
			}
		});
		setWidget(0, 0, newButton);

		final Button deleteButton = new Button("");
		personService.isAdminUser(new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					deleteButton.setText("Delete");
					deleteButton.addClickHandler(new ClickHandler() {
						@Override
						public void onClick(ClickEvent event) {
							personService.deleteRto(Integer.parseInt(at
									.getSelectedRowId()),
									new AsyncCallback<Void>() {
										@Override
										public void onFailure(Throwable caught) {
											new SimpleMessagePopup(
													"The request to delete the RTO failed.",
													null).showPopup();
										}

										@Override
										public void onSuccess(Void result) {
											at.updateTableData();
										}
									});
						}
					});
				} else {
					deleteButton.setVisible(false);
				}

			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		});
		setWidget(0, 1, deleteButton);

		setHTML(1, 0, "Name");
		setWidget(1, 1, nameTextBox);
		setHTML(1, 2, "Contact name");
		setWidget(1, 3, contactNameTextBox);
		setHTML(1, 4, "Contact position");
		setWidget(1, 5, contactPosition);
		setHTML(2, 0, "Campus");
		setWidget(2, 1, campus);
		setHTML(2, 2, "State");
		setWidget(2, 3, state);
		setWidget(3, 0, at);
		cellFormatter.setColSpan(3, 0, 4);

	}

	public void updateTableData() {
		at.updateTableData();
	}
}
